<?php
// +----------------------------------------------------------------------
// | yiran [ 张依然 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://yzncms.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 张依然 <530765310@qq.com>
// +----------------------------------------------------------------------

// +----------------------------------------------------------------------
// | 会员充值
// +----------------------------------------------------------------------
namespace app\member\controller;


use app\member\model\Member as Member_Model;
use app\Payment\model\PayAccount as PayAccount_Model;
use app\Payment\model\PaySpend as PaySpend_Model;
use app\Payment\model\PayPayment as PayPayment_Model;
use app\member\service\User;
use think\facade\Cookie;
use think\facade\Validate;

class OnlineRecharge extends MemberBase
{

    //初始化
    protected function initialize()
    {
        parent::initialize();
        header('Access-Control-Allow-Origin:*');
        $this->Member_Model = new Member_Model;
        $this->PaySpend_Model = new PaySpend_Model;
        $this->PayAccount_Model=new PayAccount_Model;
        $this->PayPayment_Model=new PayPayment_Model;
        $this->point=10;
        $this->min_money=1;
        $this->min_price=1;
    }
    //在线支付
    public function index(){
        if ($this->request->isPost()) {
            $info = $this->request->param('info');
            $code=$info['code'];
            if (empty($code) && $this->memberConfig['openverification']) {
                $this->error('验证码错误！','',['msg_title'=>'验证码错误']);
            }
            if ($this->memberConfig['openverification'] && !captcha_check($code)) {
                $this->error('验证码错误！','',['msg_title'=>'验证码错误']);
            }
            if((float)$info['price']<$this->min_price){
                $this->error('充值金额大于等于'.$this->min_price.'元','',['msg_title'=>'充值金额大于等于'.$this->min_price.'元']);
            }
            //创建订单
            $order_sn=date('YmdHis').rand(100000,999999);
            $data['trade_sn']=$order_sn;
            $data['serial_number']=date('YmdHis').rand(100000,999999);
            $data['userid']=$this->userid;
            $data['username']=$info['name'];
            $data['contactname']=$info['name'].'在线充值';
            $data['email']=$info['email'];
            $data['money']=$info['price'];
            $data['quantity']=1;
            $data['addtime']=time();
            $data['ip']=request()->ip();
            $data['usernote']=$info['name'].'['.$order_sn.']';
            $data['pay_id']=$info['pay_type'];
            $data['payment']=$info['payment'];
            if($this->PayAccount_Model->addData($data)){
                $this->success('创建订单成功', url('OnlineRecharge/pay_recharge',['order_sn'=>$order_sn]),['msg_title'=>'提交成功,进入确认页面']);
            }else{
                $this->error('创建订单！','',['msg_title'=>'提交失败']);
            }

        }else{
       $pay_res= $this->PayPayment_Model->get_all();

        $this->assign('pay_res',$pay_res);
        return $this->fetch('/onlinerecharge_index');
        }
    }
    //在线支付
    public function pay_recharge(){
        $order_sn = $this->request->param('order_sn');
        if(empty($order_sn)){
            $this->error('非法访问！');
        }
        $where=['trade_sn'=>$order_sn];
       $order_res= $this->PayAccount_Model->get_one($where);
       if(empty($order_res)){
           $this->error('非法访问！');
       }
        $this->assign('order_res',$order_res);

        return $this->fetch('/pay_recharge');
    }

    public function payment_log(){
        if ($this->request->isPost()) {
            $page = $this->request->param('page');
            $status = $this->request->param('status','');
            $start_addtime = $this->request->param('start_addtime');
            $end_addtime = $this->request->param('end_addtime');
            $limit = $this->request->param('limit');
            $data['limit']=$limit;
            $data['start_addtime']=strtotime($start_addtime);
            $data['end_addtime']=strtotime($end_addtime);

            if(empty($status)){

                $where=['userid'=>$this->userid];
            }else{
                $where=['userid'=>$this->userid,'status'=>$status];
            }

            $res=   $this->PayAccount_Model->get_page_list($where,$data);
            $res['code']=0;
            $res['count']=$res['total'];
            $res['msg']=0;
            return json($res);
        }else{
            return $this->fetch('/payment_log');
        }


    }
    public function expenses_record(){
        if ($this->request->isPost()) {
            $page = $this->request->param('page');
            $type = $this->request->param('type','0');
            $start_addtime = $this->request->param('start_addtime');
            $end_addtime = $this->request->param('end_addtime');
            $limit = $this->request->param('limit');
            $data['limit']=$limit;
            $data['start_addtime']=strtotime($start_addtime);
            $data['end_addtime']=strtotime($end_addtime);
            if($type==0){
                $where=['userid'=>$this->userid];
            }else{
                $where=['userid'=>$this->userid,'type'=>$type];
            }

            $res=$this->PaySpend_Model->get_page_list($where,$data);
            $res['code']=0;
            $res['count']=$res['total'];
            $res['msg']=0;
            return json($res);

        }else{

            return $this->fetch('/expenses_record');
        }

    }
    public function exchange(){
        if ($this->request->isPost()) {
            $act = $this->request->param('act');
            $code= $this->request->param('code');
            $msg=empty( $this->request->param('msg'))?'余额兑换点数':$this->request->param('msg');
            $money = (float)$this->request->param('money');
            $userinfo=$this->userinfo->toArray();
            //amount
            $point=$money*$this->point;
            if($money<$this->min_money){
                $this->error('验证最小金额。','',['msg_title'=>'最小金额'.$this->min_money.'元。','points'=>$point]);
            }
            if($userinfo['amount']<$money){
                $this->error('验证余额不足。','',['msg_title'=>'您的余额不足。','points'=>$point]);
            }
            switch ($act){
                case 'chek_exchange_money':

                    $this->success('验证成功', '',['msg_title'=>'&nbsp;','points'=>$point]);
                    break;
                case 'exchange':
                    //验证码
                    if (empty($code) && $this->memberConfig['openverification']) {
                        $this->error('验证码错误！','',['msg_title'=>'验证码错误','points'=>$point]);
                    }
                    if ($this->memberConfig['openverification'] && !captcha_check($code)) {
                        $this->error('验证码错误！','',['msg_title'=>'验证码错误','points'=>$point]);
                    }
                    //减少余额 ，增加点数 增加一条消费记录
                    $this->Member_Model->where(['id'=>$this->userid])->setDec('amount', $money);
                    $this->Member_Model->where(['id'=>$this->userid])->setInc('point', $point);
                    $data['creat_at']=time();
                    $data['userid']=$this->userid;
                    $data['username']=$userinfo['username'];
                    $data['serial_number']=date('YmdHis',time()).rand(100000,999999);
                    $data['type']=1;
                    $data['value']=$money;
                    $data['msg']=$msg;
                   if($this->PaySpend_Model->addData($data)) {
                       $this->success('兑换成功', '',['msg_title'=>'兑换成功','points'=>$point]);
                   }else{
                       $this->error('兑换失败！','',['msg_title'=>'兑换失败','points'=>$point]);
                   }


                    break;
                default:
                    $this->error('未知类型。','',['msg_title'=>'未知类型']);
                    break;
            }

        }else{
            return $this->fetch('/exchange');
        }

    }
    public function test(){

    }

}